<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-select size="small"  v-model="listQuery.category_id" default-first-option style="width: 180px" placeholder="所属位置"  clearable >
        <el-option v-for="(item, index) in joinArr" :key="index" :label="item.name" :value="item.category_id" />
      </el-select>-->
      <el-input
        size="small"
        v-model="listQuery.category_name"
        style="width: 200px"
        class="filter-item"
        placeholder="名称"
        clearable
      />
      <el-button
        size="small"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        查询
      </el-button>
      <el-button
        size="small"
        class="filter-item"
        style="margin-left: 10px"
        type="primary"
        icon="edit"
        @click="showCreate"
      >
        添加
      </el-button>
    </div>
    <div class="table-container">
      <el-table
        key="tableKey"
        :data="list"
        v-loading="listLoading"
        element-loading-text="加载中"
        border
        fit
        stripe
        highlight-current-row
        style="width: 90%"
      >
        <el-table-column align="center" label="编号" width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.category_id }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="类别名字" width="200">
          <template slot-scope="scope">
            <span>{{ scope.row.category_name }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="类别级别">
          <template slot-scope="scope">
            <el-tag :type="scope.row.parent_id ? '' : 'info'">
              <span>{{ scope.row.parent_id == 0 ? "一级" : "子级" }}</span>
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="有无下级" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag :type="scope.row.types ? '' : 'info'">
              <span>{{ scope.row.types == 1 ? "有" : "无" }}</span>
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status ? 'success' : 'danger'">
              <span>{{ scope.row.status == 1 ? "已显示" : "已停用" }}</span>
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="更新时间">
          <template slot-scope="scope">
            <span>{{ scope.row.updated_at }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              plain
              size="small"
              type="primary"
              @click="handleEdit(scope.row, 'edit')"
              >编辑</el-button
            >
            <el-button
              plain
              v-if="scope.row.status == 1"
              size="small"
              type="warning"
              @click="changeStatus(scope.row, 0)"
              >去停用</el-button
            >
            <el-button
              plain
              v-else
              size="small"
              type="success"
              @click="changeStatus(scope.row, 1)"
              >去发布</el-button
            >
            <el-button
              plain
              size="small"
              type="danger"
              @click="handleDel(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container text-right">
      <el-pagination
        background
        :total="pagination.total"
        :current-page="listQuery.page"
        :page-size="listQuery.limit"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form
        class="small-space"
        :model="temp"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="所属父级">
          <el-select
            size="small"
            v-model="temp.parent_id"
            default-first-option
            style="width: 180px"
            placeholder=""
            clearable
          >
            <el-option
              v-for="(item, index) in joinArr"
              :key="index"
              :label="item.category_name"
              :value="item.category_id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" required>
          <el-input
            size="small"
            v-model="temp.category_name"
            maxlength="32"
          ></el-input>
          <el-tag type="warning" effect="plain">
            * 父级为空，则默认添加为
            <font style="font-size:12px">一级</font>！</el-tag
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button v-if="dialogStatus == 'create'" type="primary" @click="save"
          >添 加</el-button
        >
        <el-button v-else type="primary" @click="save">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  List,
  Create,
  Edit,
  Del,
  Status,
  Sort,
  ct_List,
  ct_Plist,
  ct_Save,
  ct_Del,
  ct_Status,
  ct_Sort
} from "@/api/tixing";
import { getToken } from "@/utils/auth";
import { uploadPath } from "@/utils/common";

export default {
  data() {
    return {
      list: null,
      joinArr: null,
      changeIndex: -1,
      pagination: {},
      dialogFormVisible: false,
      temp: {
        category_name: null,
        parent_id: 0,
        status: "",
        types: 0
      },
      listQuery: {
        page: 1,
        limit: 10
      },
      uploadPath: uploadPath,
      dialogStatus: null,
      dialogTitle: "",
      header: {
        Authorization: getToken()
      }
    };
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  created() {
    //this.getList(),
    this.getList();
  },

  methods: {
    getList() {
      ct_List(this.listQuery).then(response => {
        var data = response.data;
        this.list = data.data;
        this.joinArr = data.data;
        this.pagination = data;
      });

      this.listLoading = false;
    },

    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },

    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },

    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },

    changeStatus(row, status) {
      ct_Status({ category_id: row.category_id, status: status }).then(
        response => {
          row.status = status;
          this.$message.success("操作成功！");
          this.getList();
        }
      );
    },

    orderHandle(row) {
      row.show = 1;
    },

    showCreate() {
      ct_Plist().then(response => {
        var data = response.data;
        this.joinArr = data;
      });

      this.temp = {
        category_id: null,
        category_name: null,
        parent_id: null,
        types: null
      };
      this.dialogTitle = "添加";
      this.dialogStatus = "create";
      (this.dialogTitle = "添加"), (this.dialogFormVisible = true);
    },

    handleEdit(row, status) {
      ct_Plist().then(response => {
        var data = response.data;
        this.joinArr = data;
      });

      this.dialogTitle = "编辑";
      this.dialogFormVisible = true;
      this.temp = row;
      if (this.temp.parent_id == 0) {
        this.temp.parent_id = "";
      }
      this.dialogStatus = status;
      //this.dialogFormVisible = false
    },

    handleDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          ct_Del({ category_id: row.category_id }).then(response => {
            if (response.code === 0) {
              this.$message({
                message: response.msg,
                type: "success"
              });
              this.dialogFormVisible = false;
              this.getList();
            } else {
              this.$message({
                message: response.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    save() {
      //20231024一级修改名字时，导致parent_id为空bug修复
      if (this.temp.parent_id == "") {
        this.temp.parent_id = 0;
      }
      ct_Save(this.temp)
        .then(response => {
          if (response.code === 0) {
            this.$message({
              message: response.msg,
              type: "success"
            });
            this.dialogFormVisible = false;
            this.getList();
          } else {
            this.$message({
              message: response.msg,
              type: "error"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>

<style>
/* .table-container {
  margin: 20px 0;
} */
.alert-container {
  margin: 20px 0;
}
</style>
